package com.kelvin.paopao.module.market.wallet.vip;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.easeui.utils.SpUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.VipPriceAdapter;
import com.kelvin.paopao.api.responseBean.VipPriceModel;
import com.kelvin.paopao.injector.components.DaggerVipComponent;
import com.kelvin.paopao.injector.modules.VipModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.market.wallet.charge.ChargeActivity;
import com.kelvin.paopao.widgets.CustomToolbar;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by MCK on 2017/5/23.
 */

public class VipActivity extends BaseActivity<IVipPresenter>
        implements IVipView {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.iv_avatar)
    CircleImageView ivAvatar;
    @BindView(R.id.tv_money_status)
    TextView tvMoneyStatus;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_vip)
    ImageView ivVip;

    @Inject
    VipPriceAdapter adapter;

    private boolean isVip;
    private String vipDate, money;

    private LinearLayoutManager manager;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_vip;
    }

    @Override
    protected void initInjector() {
        DaggerVipComponent.builder().vipModule(new VipModule(this))
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        customToolbar.showBackIcon(this);

        isVip = getIntent().getBooleanExtra("isVip", false);
        vipDate = getIntent().getStringExtra("vipDate");
        money = getIntent().getStringExtra("money");

        if (isVip) {
            ivVip.setImageResource(R.drawable.icon_is_vip);
            tvMoneyStatus.setText("你的会员将于" + vipDate.substring(0,vipDate.lastIndexOf(" "))
                    + "过期");
        } else {
            ivVip.setImageResource(R.drawable.icon_is_not_vip);
        }

        tvMoney.setText(money);

        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                VipPriceModel model = (VipPriceModel) adapter.getData().get(position);
                showBecomeVipDialog(model.getTitle(), String.valueOf(model.getMoneys()),
                        String.valueOf(model.getId()));
            }
        });
        recyclerView.setAdapter(adapter);

        Glide.with(this).load(SpUtil.getLoginAvatar(this)).into(ivAvatar);
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.getData(isRefresh);
    }

    @OnClick(R.id.tv_charge)
    public void onClick() {
        ChargeActivity.launch(this);
    }

    public static void launch(Context context, boolean isVip, String vipDate, String money) {
        Intent intent = new Intent(context, VipActivity.class);
        intent.putExtra("isVip", isVip);
        intent.putExtra("vipDate", vipDate);
        intent.putExtra("money", money);
        context.startActivity(intent);
    }


    @Override
    public void loadData(List<VipPriceModel> models) {
        adapter.setNewData(models);
    }

    private void showBecomeVipDialog(String title, String money, final String id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("提示").setMessage("是否支付" + money + "元，开通" + title);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mPresenter.becomeVip(id);
            }
        });
        builder.show();
    }
}
